﻿using System.Collections.Generic;
using BrnMall.Manager.Domain.Store;
using PetaPoco;

namespace BrnMall.Manager.Manager.Store
{
    public static class WidgetManager
    {
        public static IList<Widget> GetWidgetList(int storeId,bool isActive)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<Widget>("select * from bma_widget where storeid=@0 and isactive=@1",storeId,isActive);
            }
        }

        public static Widget GetWidget(int wid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<Widget>(wid);
            }
        }
        public static void UpdateWidget(Widget wid)
        {
            using (var db = new QDMallDatabase())
            {
                 db.Update(wid);
            }
        }
        public static Widget GetWidget(int storeId,string code)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<Widget>("select top 1 * from bma_widget where storeid=@0 and  code=@1", storeId,
                    code);
            }
        }

        public static void InsertWidget(Widget widget)
        {
            using (var db = new QDMallDatabase())
            {
                 db.Insert(widget);
            }
        }

        public static IList<WidgetConfig> GetConfigList(int wid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<WidgetConfig>("select * from bma_widgetconfig where wid=@0  order by displayOrder ",
                    wid);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="wid"></param>
        /// <param name="postion">所在的首页区域，主要针对图片组件；0：未定义， 1:首页搜索框下，2：首页导航按钮下，3：首页内容下，4：首页商品下</param>
        /// <returns></returns>
        public static IList<WidgetConfig> GetConfigShowList(int wid,int postion=0)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<WidgetConfig>("select * from bma_widgetconfig where wid=@0 and DisplayPosition=@1 and isshow=1  order by displayOrder ",
                    wid,postion);
            }
        }
        public static WidgetConfig GetConfig(int cid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<WidgetConfig>(cid);
            }
        }

        public static void InsertConfig(WidgetConfig config)
        {
            using (var db=new QDMallDatabase())
            {
                db.Insert(config);
            }
        }

        public static void UpdateConfig(WidgetConfig config)
        {
            using (var db = new QDMallDatabase())
            {
                db.Update(config);
            }
        }
        /// <summary>
        /// 更改组件的特性
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="features"></param>
        public static void UpdateWidgetCustomerFeatures(int cid, string features)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update bma_widgetconfig set CustomerFeatures=@0 where cid=@1 ", features, cid);
            }
        }

        public static void DeleteConfig(int cid)
        {
            using (var db = new QDMallDatabase())
            {
                db.Delete<WidgetConfig>(cid);
            }
        }

        public static void SortConfig(int cid, int displayOrder)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update bma_widgetconfig set displayOrder=@0 where cid=@1", displayOrder, cid);
            }
        }
    }
}
